package ee.karileet.ui.catalogue.view;

import java.util.ResourceBundle;

import com.vaadin.addon.touchkit.ui.NavigationView;
import com.vaadin.addon.touchkit.ui.VerticalComponentGroup;
import com.vaadin.event.FieldEvents.FocusEvent;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.server.ExternalResource;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;

import ee.karileet.productcatalogue.model.Product;
import ee.karileet.ui.catalogue.util.Const;
import ee.karileet.ui.catalogue.util.Translations;
import ee.karileet.ui.catalogue.view.fragment.AttributeTable;

@SuppressWarnings("serial")
public class ProductView extends NavigationView {

    public ProductView(final Product p) {
    	ResourceBundle rb = Translations.get(null, Const.TRANSLATIONS);
        setCaption(p.getName());
        
        final VerticalComponentGroup content = new VerticalComponentGroup();
        final HorizontalLayout hl = new HorizontalLayout();
       
        Embedded image = new Embedded(null, new ExternalResource(Const.APPURI + p.getImageUri()));
        
        image.setMimeType("image/jpg");
        
        final VerticalComponentGroup vl = new VerticalComponentGroup();
        TextField manufactor = new TextField(rb.getString("manufactor"));
        manufactor.setValue(p.getManufactor().getName());
        manufactor.setReadOnly(true);
        vl.addComponent(manufactor);
        
        manufactor.addFocusListener(new FocusListener() {
			@Override
			public void focus(FocusEvent event) {
				getNavigationManager().navigateTo(new ManufactorView(p.getManufactor()));  
			}
		});
        
        TextField category = new TextField(rb.getString("category"));
        category.setValue(rb.getString(p.getGroup().getName()));
        category.setReadOnly(true);
        vl.addComponent(category);

        category.addFocusListener(new FocusListener() {
			@Override
			public void focus(FocusEvent event) {
				getNavigationManager().navigateTo(new GroupView(p.getGroup()));  
			}
		});
        
        TextField type = new TextField(rb.getString("type"));
        type.setValue(p.getType());
        type.setReadOnly(true);
        vl.addComponent(type);
        
        TextField price = new TextField(rb.getString("price"));
        price.setValue(Double.toString(p.getPrice() / 100) + " " + rb.getString("currencysign"));
        price.setReadOnly(true);
        vl.addComponent(price);
        
        TextField quantity = new TextField(rb.getString("quantity"));
        quantity.setValue(Integer.toString(p.getQuantity()));
        quantity.setReadOnly(true);
        vl.addComponent(quantity);
        
        TextArea description = new TextArea(rb.getString("description"));
        description.setValue(p.getDescription());
        description.setReadOnly(true);
        vl.addComponent(description);
        
        //omit if not set
        if (p.getWebpage().length() > 0) {
	        TextField web = new TextField(rb.getString("webpage"));
	        web.setValue(p.getWebpage());
	        web.setReadOnly(true);
	        vl.addComponent(web);
        }
        vl.setSizeFull();
        hl.addComponent(vl);
        hl.addComponent(image);
        hl.setSizeFull();
        content.addComponent(hl);
        //only show table when attributes exist
        if (p.getAttributes().size() > 0) {
	        AttributeTable attributeTable = new AttributeTable(p.getAttributes());
	        Table specs = attributeTable.buildComponent();
	        specs.setWidth("100%");
	        content.addComponent(specs);
        }
        setContent(content);
    }

}
